'''
@Version: 0.0.1
@Author: ider
@Date: 2020-03-31 14:30:29
@LastEditors: ider
@LastEditTime: 2020-03-31 15:14:06
@Description: sea 数据的校对和迁移，从 223 到 220,分裂的合并为一个集合,转移完成后需 将 wikipedia_category_nodes_v2 替换为 wikipedia_category_nodes_lv2
'''

import pickle
import  pymongo
from util import Sea
import logging
import logging.config
import time
import zlib,sys
import numpy as np
from config import SEA_URL, MONGO_URL

logging.config.fileConfig('logging.conf')

Table_old = pymongo.MongoClient(MONGO_URL).small_world.wikipedia_category_nodes_v2
Table_new = pymongo.MongoClient(MONGO_URL).small_world.wikipedia_category_nodes_lv2


SEA_old = Sea('http://192.168.1.223:8880')
SEA_new = Sea("http://192.168.1.220:8880")
def handle(year):
    if year == 2020:
        # 直接转移
        for doc in Table_old.find({'year':year}):
            logging.info(f'S,{year},{doc["_id"]}')
            try:
                _,_,old_dump = SEA_old.get(doc['_id'],'small_world_one_lv2')
                pickle.loads(zlib.decompress(old_dump))
                SEA_new.put(doc['_id'],'small_world_one_lv2',old_dump)
                Table_new.insert_one(doc)
                Table_old.delete_one(doc)
                SEA_old.delete(doc['_id'],'small_world_one_lv2')
            except Exception as e:
                logging.exception(e)
                continue
    else:
        # 其他年份，需处理数据后再转移
        for doc in Table_old.find({'year':year}):
            logging.info(f'S,{year},{doc["_id"]}')
            try:
                _,_,data_1 = SEA_old.get(doc['_id'],'small_world_target_direct')
                _,_,data_2 = SEA_old.get(doc['_id'],'small_world_distance_direct')
                pd1 = pickle.loads(zlib.decompress(data_1))
                pd2 = pickle.loads(zlib.decompress(data_2))
                if not isinstance(pd1,list):
                    pd1,pd2 = pd2,pd1
                    logging.info(f'反向,{year},{doc["_id"]}')

                d_t = zlib.compress(pickle.dumps((pd2,pd1)))
                # sharp,target = pickle.loads(zlib.decompress(data))
                SEA_new.put(doc['_id'],'small_world_one_lv2',d_t)
                Table_new.insert_one(doc)
                Table_old.delete_one(doc)
                SEA_old.delete(doc['_id'],'small_world_target_direct')
                SEA_old.delete(doc['_id'],'small_world_distance_direct')


            except Exception as e:
                logging.exception(e)
                continue

def main():
    # handle(2020)
    for year in range(2007,2021):
        handle(year)

if __name__ =="__main__":
    main()